<template>
  <div class="container">
    <div class="title">
      <h2>维修记录管理</h2>
      <h5>记录对资产设备的维修情况记录</h5>
    </div>
    <!-- 标题 -->
    <el-card class="box-card">
      <el-form ref="formData" :model="formData" class="demo-form-inline">
        <el-form-item label="小区名称">
          <el-select v-model="formData.community_code" placeholder="请输入">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.code"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备编码">
          <el-input v-model="formData.device_code" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="最后维护时间">
          <div class="block">
            <el-date-picker
              v-model="formData.last_maintain_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </div>
        </el-form-item>
        <el-form-item label="下次维护时间">
          <div class="block">
            <el-date-picker
              v-model="formData.next_maintain_time"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </div>
        </el-form-item>
        <el-form-item class="btn">
          <el-button
            style="float: right; padding: 10px 20px"
            type="primary"
            size="small"
            @click="searchmaintain"
          >查询</el-button>
          <el-button
            style="float: right; padding: 10px 20px;margin-right:10px;z-index=100"
            size="small"
            @click="reset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 表格 -->
    <el-card class="tableCard">
      <el-button
        v-permission="{ name: 'create', path: $route.path }"
        style="float: right; padding: 10px 20px; margin-right: 80px"
        type="primary"
        size="small"
        @click="addmaintainlist"
      >新增维修记录</el-button>
      <el-table v-loading="loading" :data="list" style="width: 100%">
        <el-table-column prop="community.name" label="小区名称" width="70px" />
        <el-table-column prop="device" label="设备名称" width="70px">
          <template #default="{ row }">
            <span>{{ row.device ? row.device.name : "未知" }}</span>
          </template>
        </el-table-column>

        <el-table-column prop="device_code" label="设备编码" width="200px" />
        <el-table-column prop="unit" label="维修单位名称" width="100px" />
        <el-table-column prop="contacts" label="维修人名称" width="100px" />
        <el-table-column prop="photo" label="照片">
          <template #default="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              :src="`http://124.223.14.236:8881${row.photo}`"
              :preview-src-list="[`http://124.223.14.236:8881${row.photo}`]"
            />
            <!-- <img style="width:100px" :src="`http://124.223.14.236:8881${row.photo}`"> -->
          </template>
        </el-table-column>
        <el-table-column prop="contacts_tel" label="联系方式" width="100px" />
        <el-table-column prop="remark" label="备注" width="100px" />
        <el-table-column prop="last_maintain_time" label="最后维护时间">
          <template #default="{ row }">
            {{ row.last_maintain_time | parseTime }}
          </template>
        </el-table-column>
        <el-table-column prop="next_maintain_time" label="下次维护时间">
          <template #default="{ row }">
            {{ row.next_maintain_time | parseTime }}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template #default="{ row }">
            <el-button
              v-permission="{ name: 'update', path: $route.path }"
              type="text"
              size="small"
              @click="showmaintainlist(row.id)"
            >编辑</el-button>
            <el-button
              v-permission="{ name: 'destroy', path: $route.path }"
              type="text"
              size="small"
              @click="delmaintainlist(row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="bottom">
        <el-pagination
          :current-page="formData.current"
          :page-sizes="[5, 10, 30, 40]"
          :page-size="formData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <AddForm ref="dialog" :drawer.sync="drawer" />
  </div>
</template>

<script>
import {
  getCommunityApi,
  getmaintainlistApi,
  delmaintainlistApi
} from '@/api/device/maintain'
import AddForm from './components/addForm.vue'
export default {
  name: 'VueAdminTemplateMasterProject',
  components: {
    AddForm
  },
  data() {
    return {
      // 搜索 表单
      formData: {
        current: 1,
        pageSize: 10,
        device_code: '',
        community_code: '',
        last_maintain_time: '',
        next_maintain_time: ''
      },
      options: [],
      list: [],
      total: 0,
      loading: false,
      drawer: false
    }
  },
  computed: {},

  created() {
    this.initData()
    this.getmaintainlist()
  },
  methods: {
    /**
     * 重置
     */
    reset() {
      this.formData = {
        current: 1,
        pageSize: 10,
        device_code: '',
        community_code: '',
        last_maintain_time: '',
        next_maintain_time: ''
      }
      this.getmaintainlist()
    },
    /**
     * 搜索
     */
    async searchmaintain() {
      if (this.formData.last_maintain_time) {
        this.formData.last_maintain_time = this.formData.last_maintain_time
          .map((item) => {
            return +new Date(item)
          })
          .join(',')
      }
      if (this.formData.next_maintain_time) {
        this.formData.next_maintain_time = this.formData.next_maintain_time
          .map((item) => {
            return +new Date(item)
          })
          .join(',')
      }
      const { data: res } = await getmaintainlistApi({
        pageSize: 999,
        current: 1,
        last_maintain_time: this.formData.last_maintain_time,
        next_maintain_time: this.formData.next_maintain_time,
        community_code: this.formData.community_code,
        device_code: this.formData.device_code
      })
      this.list = res.rows
      this.total = res.count
    },
    /**
     * 回显
     */
    showmaintainlist(id) {
      this.$refs.dialog.getmaintainlist1(id)
      // console.log(id)
    },
    /**
     * 删除
     */
    delmaintainlist(id) {
      this.$confirm('确认要删除吗?', '温馨提示')
        .then(async() => {
          await delmaintainlistApi(id) // 调用删除接口
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          // 重新加载数据
          this.getmaintainlist()

          this.$message.success('删除成功')
        })
        .catch((error) => {
          console.log(error)
        })
    },
    /**
     * 获取小区
     */
    async initData() {
      const res = await getCommunityApi()
      this.options = res.data
      // console.log(this.options)
    },
    /**
     * 获取维修记录管理列表
     */
    async getmaintainlist() {
      this.loading = true
      const {
        data: { rows, count }
      } = await getmaintainlistApi({
        current: this.current,
        pageSize: this.pageSize
      })
      this.list = rows
      this.total = count
      this.loading = false
    },
    handleSizeChange(val) {
      this.current = 1
      this.pageSize = val
      this.getmaintainlist()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getmaintainlist()
    },
    addmaintainlist() {
      this.drawer = true
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 40px;
  .title {
    display: flex;
    align-items: center;
    h2 {
      margin-right: 20px;
    }
    h5 {
      font-weight: 400;
      color: #9c9c9d;
      vertical-align: middle;
    }
  }
  .demo-form-inline {
    display: flex;
  }
  .el-form-item {
    margin-right: 40px;
    ::v-deep .el-input__inner {
      height: 35px;
      line-height: 35px;
      width: 200px;
    }
  }
  .btn {
    padding-top: 40px;
  }
  .box-card {
    margin-bottom: 10px;
  }
  .bottom {
    display: flex;
    align-items: center;
    font-size: 13px;
    justify-content: end;
  }
}
</style>
